/*
 * $Id$
 *
 * Copyright 2006- Revolution Health Group.  All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Revolution Health Group.  (Confidential Information).
 * You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license 
 * agreement you entered into with Revolution Health Group.
 *
 */
package org.javason.jsonrpc;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import org.javason.json.BeanToJsonSerializer;

import junit.framework.TestCase;

public class TestJsonRpcBeanParam extends TestCase {
	
	public void testJsonInterface3() {
		System.out.println("RUNNING testJsonInterface()");
		String requestURI = "/world";
		MockHttpServletRequest request = new MockHttpServletRequest("POST", requestURI);
		MockHttpServletResponse response = new MockHttpServletResponse();
		request.setPathInfo(requestURI);
		
		JSONObject doSomething = new JSONObject();
		JSONObject testId = new JSONObject();
		try {
			testId.put("someCtx", false);
			doSomething.put("id", testId);
		
			doSomething.put("method", "doSomething");
			JSONArray param1 = new JSONArray();
			JsonBean tjb = new JsonBean();
			tjb.setTest("testing 3 4 5");
			tjb.setTestDbl((double) 11.2);
			tjb.setTestLng((long)11L);
			tjb.setTestType(JsonBean.TEST_TYPE.going_in);
			
			List<String> testArray = new ArrayList<String>();
			testArray.add("something in the array");
			tjb.setTestStrings(testArray);
			
			JsonBean2 bean2 = new JsonBean2();
			bean2.setTestBoolean(false);
			tjb.setTestJsonBean2(bean2);
			
			JsonBean2 bean2_2 = new JsonBean2();
			bean2_2.setTestBoolean(true);
			tjb.setTestJsonBean2Array(Arrays.asList(bean2_2));
			
			JSONObject paramObj = BeanToJsonSerializer.serializeToJsonObject(tjb);
			System.out.println(paramObj.toString());
			
			// {"testStrings":["something in the array"],"testJsonBean2":{"testBoolean":false},"testDbl":11.2,"testLng":11,"testType":"going_in","testJsonBean2Array":[{"testBoolean":true}],"test":"testing 3 4 5"}
			assertEquals(7, paramObj.size());
			assertTrue(paramObj.getJSONArray("testStrings").size() == 1);
			assertTrue(paramObj.getJSONObject("testJsonBean2").getBoolean("testBoolean") == false);
			assertTrue(paramObj.getDouble("testDbl") == 11.2);
			assertTrue(paramObj.getLong("testLng") == 11);
			assertEquals(paramObj.getString("testType"), JsonBean.TEST_TYPE.going_in.name());
			assertEquals(paramObj.getString("test"), "testing 3 4 5");
			assertTrue(paramObj.getJSONArray("testJsonBean2Array").getJSONObject(0).getBoolean("testBoolean") == true);

			param1.add(0, paramObj);
			doSomething.put("params", param1);
		
			request.setContent(doSomething.toString().getBytes("UTF-8"));
		} catch (JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			assertTrue(false);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
		
		try {
			JsonRpcServiceExporter exporter = new JsonRpcServiceExporter();
			exporter.setServiceBean(new JsonInterface());
			
			ModelAndView mv = exporter.handleRequest(request, response);
			
			JsonRpcServiceView iv = new JsonRpcServiceView();
			iv.render(mv.getModel(), request, response);
			System.out.println(response.getContentAsString());
			
			JSONObject responseObj = JSONObject.fromObject(response.getContentAsString());
			assertTrue(responseObj.has("result"));
			assertTrue(JSONUtils.isNull(responseObj.opt("error")));
			assertTrue(responseObj.getJSONObject("id").toString().equals(testId.toString()));
			
			JSONObject paramObj = responseObj.getJSONObject("result");
			assertTrue(paramObj.size() == 7);
			assertTrue(paramObj.getJSONArray("testStrings").size() == 1);
			assertTrue(paramObj.getJSONObject("testJsonBean2").getBoolean("testBoolean") == false);
			assertTrue(paramObj.getDouble("testDbl") == 11.2);
			assertTrue(paramObj.getLong("testLng") == 11);
			assertEquals(paramObj.getString("testType"), JsonBean.TEST_TYPE.testing.name());
			assertEquals(paramObj.getString("test"), "came in"); // set in Interface class
			assertTrue(paramObj.getJSONArray("testJsonBean2Array").getJSONObject(0).getBoolean("testBoolean") == true);

			
		} catch (Exception e1) {
			e1.printStackTrace();
			assertTrue(false);
		}
		System.out.println("--");
	}

}
